package com.sdry.web.controller.hpf;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.hpf.Beat;
import com.sdry.model.hpf.Page;
import com.sdry.service.hpf.BeatService;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName: BeatController 
 * @Description:节拍Controller
 * @Author hpf
 * @Date 2018年9月21日 上午10:31:07 
 * @version 1.0
 */
@RequestMapping(value="/beatMana")
@Controller
public class BeatController {

	@Resource
	private BeatService beatService;
	
	/**
	 * 分页查询
	 * @param page 分页条件
	 * @param beat 查询条件
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/listPageBeat")
	public void listPageBeat(Page page,Beat beat,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<>();
		map.put("limit", page.getLimit());
		map.put("page", page.getPage());
		if (beat.getBnum() != null) {
			map.put("bnum", beat.getBnum());
		} else {
			map.put("bnum", "");
			beat.setBnum("");
		}
		int count = beatService.countBeat(beat);
		List<Beat> beatList = beatService.listPageBeat(map);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(beatList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		ResponseUtil.write(response, jsonObject);
	}
	
	/**
	 * 保存节拍
	 * @param beat
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/saveBeat")
	public void saveBeat(Beat beat,HttpServletResponse response) throws Exception{
		int isSuccess = 0;
		if (beat.getBid() == null || "".equals(beat.getBid())) {
			isSuccess = beatService.insertBeat(beat);
		} else {
			isSuccess = beatService.updateBeat(beat);
		}
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 删除节拍
	 * @param str 节拍ID组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteBeat")
	public void deleteBeat(@RequestParam("str") String str,HttpServletResponse response) throws Exception{
		int isSuccess = beatService.deleteBeat(str);
		ResponseUtil.write(response, isSuccess);
	}
	
	/**
	 * 校验是否删除节拍
	 * @param str 节拍ID组成的字符串
	 * @param response
	 * @throws Exception 
	 */
	@RequestMapping(value="/deleteBeatOfVerify")
	public void deleteBeatOfVerify(@RequestParam("str") String str,HttpServletResponse response) throws Exception{
		int isSuccess = beatService.deleteBeatOfVerify(str);
		ResponseUtil.write(response, isSuccess);
	}
}
